package com.yanqu.road.logic.sort;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Comparator;

public class CrossRankSort implements Comparator<CrossUserRank> {
    @Override
    public int compare(CrossUserRank o1, CrossUserRank o2) {
        int result = o2.getValue().compareTo(o1.getValue());
        if (result != 0) {
            return result;
        }

        if (o1.getBigRankType() == eBigRankType.Skin.getValue() ||
                o1.getBigRankType() == eBigRankType.Patrons.getValue() ||
                o1.getBigRankType() == eBigRankType.Beauty.getValue()
        ) {
            JSONObject o2JsonObject = JSONObject.parseObject(o2.getParam());
            JSONObject o1JsonObject = JSONObject.parseObject(o1.getParam());
            // 值
            result = new BigInteger(StringUtils.isNullOrEmpty(o2JsonObject.getString("value")) ? "0" : o2JsonObject.getString("value")).compareTo(new BigInteger(StringUtils.isNullOrEmpty(o1JsonObject.getString("value")) ? "0" : o1JsonObject.getString("value")));
            if (result != 0) {
                return result;
            }
        }

        if (o1.getBigRankType() == eBigRankType.Vehicle.getValue()) {
            JSONObject o1Object = JSONObject.parseObject(o1.getParam());
            JSONObject o2Object = JSONObject.parseObject(o2.getParam());
            // 赚钱加成比较
            int o1Add = StringUtils.isNullOrEmpty(o1Object.getString("addition")) ? 0 : o1Object.getInteger("addition");
            int o2Add = StringUtils.isNullOrEmpty(o2Object.getString("addition")) ? 0 : o2Object.getInteger("addition");
            result = o2Add - o1Add;
            if (result != 0) {
                return result;
            }
        }

        long time = o1.getLastUpdateTime() - o2.getLastUpdateTime();
        result = time > 0 ? 1 : (time == 0 ? 0 : -1);
        return result;
    }
}
